# Maintainer: Miloš Komarčević <miloskomarcevic@aim.com>

_realname=highway
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=1.2.0
pkgrel=1
pkgdesc="C++ library for SIMD (Single Instruction, Multiple Data) (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clangarm64')
url='https://github.com/google/highway'
msys2_repository_url=${url}
msys2_references=(
  'purl: pkg:github/google/highway'
)
license=('spdx:Apache-2.0')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja")
#checkdepends=("${MINGW_PACKAGE_PREFIX}-gtest")
source=("${msys2_repository_url}/releases/download/${pkgver}/${_realname}-${pkgver}.tar.gz"{,.asc}
        "https://patch-diff.githubusercontent.com/raw/google/highway/pull/2229.patch")
sha256sums=('58e9d5d41d6573ad15245ad76aec53a69499ca7480c092d899c4424812ed906f'
            'SKIP'
            '2858383512311bb50aab690f26b0cb4745a888b31005e2340d8a778e6373aa7a')
validpgpkeys=('14D827B22124EB2FE748C3B6EE0F9065BCB8B678')

prepare() {
  cd "${srcdir}"/${_realname}-${pkgver}
  
  # https://github.com/google/highway/pull/2229
  patch -Np1 -i "${srcdir}"/2229.patch
}

build() {
  declare -a extra_config
  if check_option "debug" "n"; then
    extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  # Static build
  mkdir -p "${srcdir}"/build-${MSYSTEM}-static && cd "${srcdir}"/build-${MSYSTEM}-static

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    ${MINGW_PREFIX}/bin/cmake \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
      "${extra_config[@]}" \
      -DBUILD_SHARED_LIBS=OFF \
      -DBUILD_TESTING=OFF \
      ../${_realname}-${pkgver}

  ${MINGW_PREFIX}/bin/cmake --build .

  # Shared build
  mkdir -p "${srcdir}"/build-${MSYSTEM}-shared && cd "${srcdir}"/build-${MSYSTEM}-shared

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    ${MINGW_PREFIX}/bin/cmake \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
      "${extra_config[@]}" \
      -DBUILD_SHARED_LIBS=ON \
      -DBUILD_TESTING=OFF \
      ../${_realname}-${pkgver}

  ${MINGW_PREFIX}/bin/cmake --build .
}

# Must build w/ -DBUILD_TESTING=ON and -DHWY_SYSTEM_GTEST=ON above
#check() {
#  cd "${srcdir}"/build-${MSYSTEM}-static
#
#  ${MINGW_PREFIX}/bin/cmake --build . --target test
#}

package() {
  # Static install
  cd "${srcdir}"/build-${MSYSTEM}-static

  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake --install .

  # Shared install
  cd "${srcdir}"/build-${MSYSTEM}-shared

  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake --install .

  install -Dm644 "${srcdir}"/${_realname}-${pkgver}/LICENSE "${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE
}
